package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author thomasyang
 * @since 2023-12-11
 */
//@CrossOrigin
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {


    @Autowired
    public EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;


    @ApiOperation("/添加小节")
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){

        boolean save = eduVideoService.save(eduVideo);

        return R.ok();
    }


    @ApiOperation("/删除小节")
    @DeleteMapping("/delete/{id}")
    public R deleteVideo(@PathVariable String id){

        EduVideo eduVideo = eduVideoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断小节里面是否有视频id
        if(!StringUtils.isEmpty(videoSourceId)){

            System.out.println(videoSourceId);
            R result = vodClient.removeVideoById(videoSourceId);

            if (result.getCode() == 20001){
                throw new GuliException(20001, "删除视频失败,熔断");
            }
        }

        boolean remove = eduVideoService.removeById(id);

        return R.flagCheck(remove);

    }

}

